﻿using UnityEngine;
using System.Collections;
 
using System;
//using System.Data;
//using MySql.Data.MySqlClient;
 

public class SqlCCL : MonoBehaviour {
/*
    public static SqlCCL _instance;

    public Transform Parent;
    public GameObject message;
	// Update is called once per frame
    public static MySqlConnection dbConnection;
    static string host = "55b250ba926b7.sh.cdb.myqcloud.com";
    static string id = "root";
    static string pwd = "ldabgy1314";
    static string database = "test";
    private GameObject go;
    private selfCCL self;
    private int count;
    private string str;

    void Awake() 
    {
        _instance = this;
    }
    void Start() 
    {
        OpenSql();
        DataSet ds = SelectWhere();
        if (ds != null)
        {

            DataTable table = ds.Tables[0];

            foreach (DataRow row in table.Rows)
            {
                count++;
                go = GameObject.Instantiate(message, Vector3.zero, Quaternion.identity) as GameObject;
                go.transform.parent = Parent;
                self=go.GetComponent<selfCCL>();
                int index = 0;
                
                
               // int count;
                foreach (DataColumn column in table.Columns)
                {
                    Debug.Log(row[column]);
                    if (index==0)
                    {
                        str = row[column].ToString();
                        index += 1;
                    }
                    else
                    {
                        self.setValue("TOP " + count.ToString(), row[column].ToString(), str);
                    }
                }
            }
        }	
    }
    public static void OpenSql()
    {

        try
        {
            string connectionString = string.Format("Server = {0};port={4};Database = {1}; User ID = {2}; Password = {3};", host, database, id, pwd, "7211");
            dbConnection = new MySqlConnection(connectionString);
            dbConnection.Open();
        }
        catch 
        {
            print("访问排行榜失败了！");

        }

    }
    public void Close()
    {

        if (dbConnection != null)
        {
            dbConnection.Close();
           dbConnection.Dispose();
           dbConnection = null;
        }

    }
    public void Sunmit() 
    {
        inser(PlayerPrefs.GetString("name"),playerPrefer.count);
    }
    public  DataSet ExecuteQuery(string sqlString)
    {
        if (dbConnection.State == ConnectionState.Open)
        {
            DataSet ds = new DataSet();
            try
            {

                MySqlDataAdapter da = new MySqlDataAdapter(sqlString, dbConnection);
                da.Fill(ds);

            }
            catch (Exception ee)
            {
                throw new Exception("SQL:" + sqlString + "/n" + ee.Message.ToString());
            }
            finally
            {
            }
            return ds;
        }
        return null;
    }
    //数据查询
    public DataSet SelectWhere(string tableName, string[] items, string[] col, string[] operation, string[] values)
    {

        if (col.Length != operation.Length || operation.Length != values.Length)
        {

            throw new Exception("col.Length != operation.Length != values.Length");

        }

        string query = "SELECT " + items[0];

        for (int i = 1; i < items.Length; ++i)
        {

            query += ", " + items[i];

        }

        query += " FROM " + tableName + " WHERE " + col[0] + operation[0] + "'" + values[0] + "' ";

        for (int i = 1; i < col.Length; ++i)
        {

            query += " AND " + col[i] + operation[i] + "'" + values[0] + "' ";

        }

        return ExecuteQuery(query);

    }
    public DataSet SelectWhere()
    {

        string query = "SELECT * from fkfmg order by count DESC LIMIT 100";       
        return ExecuteQuery(query);
    }
    public DataSet inser(string name,int count)
    {

        string query = "INSERT into fkfmg(name,count)VALUES("+name+",+"+count+"+)";
        return ExecuteQuery(query);
    } */
}
